﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using Models;

namespace Utilities
{
    public class DB : DbContext
    {
        public DbSet<Stock> Stocks { get;set;}
        public DbSet<Area> Areas { get; set; }
        public DbSet<Warehouse> Warehouses { get; set; }
        public DbSet<Category> Categories { get; set; }
        public DbSet<Quality> Qualities { get; set; }
        public DbSet<StockChangeType> StockChangeTypes { get; set; }
        public DbSet<StockChangeRecord> StockChangeRecords { get; set; } 
   
        public DB()
        {
            //this.Configuration.LazyLoadingEnabled = false;
            Database.SetInitializer<DB>(new ProcessInitializer());
            this.Configuration.ValidateOnSaveEnabled = false;
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //this.Configuration.LazyLoadingEnabled = false;
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }  
    }

    public class ProcessInitializer : DropCreateDatabaseIfModelChanges<DB>
    {
        protected override void Seed(DB context)
        {
            var areas = new List<Area>
            {
                new Area { AreaName = "松隐"},
                new Area { AreaName = "吕巷" },
                new Area { AreaName = "张堰" },
                new Area { AreaName = "兴塔" },
                new Area { AreaName = "新农" },
                new Area { AreaName = "漕泾" }
                
            };
            areas.ForEach(s => context.Areas.Add(s));
            context.SaveChanges();

            var categories = new List<Category>
            {
                new Category { CategoryName = "大麦" },
                new Category { CategoryName = "水稻" },
                new Category { CategoryName = "玉米" },
                new Category { CategoryName = "土豆" },
                new Category { CategoryName = "生菜" },
                new Category { CategoryName = "豆角" }
                
            };
            categories.ForEach(c => context.Categories.Add(c));

            var qualities = new List<Quality>
            {
                new Quality { QualityName = "17%水分" },
                new Quality { QualityName = "18%水分" },
                new Quality { QualityName = "19%水分" },
                new Quality { QualityName = "20%水分" },
                new Quality { QualityName = "21%水分" },
                new Quality { QualityName = "22%水分" }
                
            };
            qualities.ForEach(q => context.Qualities.Add(q));
            context.SaveChanges();
          

               
            Random rid = new Random();
            int start = 1; int end = 7;

            Random rvolume = new Random();
            int startV = 30; int endV= 100;
            var warehouses = new List<Warehouse>();

            for (int i = 0; i < 30; i++)
            {
                warehouses.Add( new Warehouse { TotalVolume=rid.Next(startV,endV),   WarehouseName  = "仓"+i.ToString(), AreaID=rid.Next(start,end) });
            }
            warehouses.ForEach(w => context.Warehouses.Add(w));
            context.SaveChanges();

            var stocks = new List<Stock>();
            Random r16 = new Random();
            int r16s = 1; int r16e = 6;

            Random r112 = new Random();
            int r112s = 1; int r112e = 12;
            Random r130 = new Random();
            int r130s = 1; int r130e = 30;
            for (int i = 0; i < 200; i++)
            {
                var warehouseID = r130.Next(r130s, r130e); 
                var areaID=CH.GetDataById<Warehouse>(warehouseID).AreaID;
                stocks.Add(new Stock { Volume = r16.Next(r16s, r16e), WarehouseID = warehouseID, AreaID=areaID, QualityID = r16.Next(r16s, r16e), CategoryID = r16.Next(r16s, r16e), StockName = "库存品" + i.ToString() });
            }
            stocks.ForEach(s => context.Stocks.Add(s));
            context.SaveChanges();
        }
    }
}